﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using LibNiconico.NiconicoApi.Internal;
using LibNiconico.NiconicoApi.Extentions;

namespace LibNiconico.NiconicoApi.PlayerStatus
{
    public sealed partial class NicoPlayerStatus
    {
        public sealed partial class InternalClasses
        {
            /// <summary>
            /// 放送ストリーム情報
            /// </summary>
            public sealed class Stream
            {
                /// <summary>
                /// ID
                /// </summary>
                public string Id { get; private set; }

                /// <summary>
                /// タイトル
                /// </summary>
                public string Title { get; private set; }

                /// <summary>
                /// 詳細
                /// </summary>
                public string Description { get; private set; }

                /// <summary>
                /// 提供者タイプ
                /// </summary>
                public string ProviderType { get; private set; }

                /// <summary>
                /// 放送コミュニティ
                /// </summary>
                public string DefaultCommunity { get; private set; }

                /// <summary>
                /// 海外放送
                /// </summary>
                public int International { get; private set; }

                /// <summary>
                /// 接続したユーザが放送主か判定
                /// </summary>
                public bool IsOwner { get; private set; }

                /// <summary>
                /// 放送主ユーザID
                /// </summary>
                public string OwnerId { get; private set; }

                /// <summary>
                /// 放送主の名前
                /// </summary>
                public string OwnerName { get; private set; }

                /// <summary>
                /// 予約枠かの判定
                /// </summary>
                public bool IsReserved { get; private set; }

                /// <summary>
                /// 視聴者数
                /// </summary>
                public int WatchCount { get; private set; }

                /// <summary>
                /// コメント数
                /// </summary>
                public int CommentCount { get; private set; }

                /// <summary>
                /// 開始時間?
                /// </summary>
                public DateTime BaseTime { get; private set; }

                /// <summary>
                /// 開場時間
                /// </summary>
                public DateTime OpenTime { get; private set; }

                /// <summary>
                /// 放送開始時間
                /// </summary>
                public DateTime StartTime { get; private set; }

                /// <summary>
                /// 終了予定時間
                /// </summary>
                public DateTime EndTime { get; private set; }

                /// <summary>
                /// ツイッタータグ
                /// </summary>
                public string TwitterTag { get; private set; }

                internal Stream() 
                {
                    this.Id = "";
                    this.Title = "";
                    this.Description = "";
                    this.ProviderType = "";
                    this.DefaultCommunity = "";
                    this.International = 0;
                    this.IsOwner = false;
                    this.IsReserved = false;
                    this.WatchCount = 0;
                    this.CommentCount = 0;
                    this.BaseTime = default(DateTime);
                    this.OpenTime = default(DateTime);
                    this.StartTime = default(DateTime);
                    this.EndTime = default(DateTime);
                    this.TwitterTag = "";
                }

                internal static Stream Parse(XElement element)
                {
                    Stream stream = XElementExtention.Parse(element, "stream", item =>
                    {
                        return new Stream
                        {
                            Id = XElementExtention.GetElementAsString(item, "id"),
                            Title = XElementExtention.GetElementAsString(item, "title"),
                            Description = XElementExtention.GetElementAsString(item, "Description"),
                            ProviderType = XElementExtention.GetElementAsString(item, "provider_type"),
                            DefaultCommunity = XElementExtention.GetElementAsString(item, "default_community"),
                            IsOwner = XElementExtention.GetElementAsBool(item, "is_owner"),
                            OwnerId = XElementExtention.GetElementAsString(item, "owner_id"),
                            OwnerName = XElementExtention.GetElementAsString(item, "owner_name"),
                            IsReserved = XElementExtention.GetElementAsBool(item, "is_reserved"),
                            WatchCount = XElementExtention.GetElementAsInt(item, "watch_count"),
                            CommentCount = XElementExtention.GetElementAsInt(item, "comment_count"),
                            BaseTime = DateTimeExtention.FromUnixTime(XElementExtention.GetElementAsLong(item, "base_time")),
                            StartTime = DateTimeExtention.FromUnixTime(XElementExtention.GetElementAsLong(item, "start_time")),
                            EndTime = DateTimeExtention.FromUnixTime(XElementExtention.GetElementAsLong(item, "end_time")),
                            OpenTime = DateTimeExtention.FromUnixTime(XElementExtention.GetElementAsLong(item, "open_time")),
                            International = XElementExtention.GetElementAsInt(item, "international"),
                            TwitterTag = XElementExtention.GetElementAsString(item, "twitter_tag")
                        };
                    });

                    return stream == null ? new Stream() : stream;
                }
            }
        }
    }
}
